package com.zq.assistant.func.apifactory.api;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by zhangqiang on 17-7-14.
 */

public class LifeCycleBinderImpl<R> implements LifeCycleBinder<R> {

    private LifeCycle lifeCycle;

    public LifeCycleBinderImpl(LifeCycle lifeCycle) {
        this.lifeCycle = lifeCycle;
    }

    @Override
    public R bindLifeCycle(final LifeCycle lifeCycle) {

        Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{LifeCycle.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object returnValue = method.invoke(lifeCycle,args);
                method.invoke(LifeCycleBinderImpl.this.lifeCycle,args);
                return returnValue;
            }
        });
    }

    private class InnerInvocationHandler implements InvocationHandler{

        private LifeCycle lifeCycle;

        public InnerInvocationHandler(LifeCycle lifeCycle) {
            this.lifeCycle = lifeCycle;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            method.invoke()
            return null;
        }
    }
}
